package comu.hnevc.zh.testdemo.dialog;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import comu.hnevc.zh.testdemo.R;
import comu.hnevc.zh.testdemo.base.BaseUrl;
import comu.hnevc.zh.testdemo.bean.Car;
import comu.hnevc.zh.testdemo.bean.Light;

public class TrafficlightDialog extends AccountDialog {
    Context mContext;
    List<Light> mLightlist;
    private EditText setred;
    private EditText setgreen;
    private EditText setyellow;

    public TrafficlightDialog(Context context, List LightList) {
        super(context, LightList);
        mContext=context;
        mLightlist=LightList;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.trafficlight_dialog);
        setred=findViewById(R.id.et_red_traffic);
        setgreen=findViewById(R.id.et_green_traffic);
        setyellow=findViewById(R.id.et_yellow_traffic);
        for (int i=0;i<mLightlist.size();i++){
            Light light=mLightlist.get(i);
            if (light.isNeedcharge()==true){}
            shezhi(light);
        }
    }

    private void shezhi(Light light) {
        SharedPreferences spf=getContext().getSharedPreferences("name",Context.MODE_PRIVATE);
        String username=spf.getString("UserName","user2");
        JSONObject object=new JSONObject();
        try {
            object.put("TrafficLightId",light.getRoadnum());
            object.put("RedTime",Integer.valueOf(setred.getText().toString()));
            object.put("YellowTime",Integer.valueOf(setyellow.getText().toString()));
            object.put("GreenTime",Integer.valueOf(setgreen.getText().toString()));
            object.put("UserName",username);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestQueue mQueue=Volley.newRequestQueue(getContext());
        JsonObjectRequest jor=new JsonObjectRequest(Request.Method.POST, BaseUrl.BASEURL + BaseUrl.SetTrafficLightConfig, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Toast.makeText(mContext,"设置成功",Toast.LENGTH_SHORT).show();
                dismiss();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        mQueue.add(jor);
    }
}
